<!DOCTYPE html>

<html lang="zh-CN">
<head>
    <%layout("/WEB-INF/view/common/inc/admin_head.html",{title:'', styles:'bootstrap,font-awesome,animate,style,bootstrap-table,layui-date,auto-complete,dtvalidate,layui,chosen'}){%>
    <style type="text/css">
        body{padding-top:10px;}
        *{margin:0;padding:0;list-style-type:none;}
        a,img{border:0;}
        .demo h2{font-size:16px;color:#3366cc;height:30px;}
        .demo li{float:left;}
        .has-no{color:#3366cc;}
        .has-success{color: #1ab394;}
        .has-warning{color: #f8ac59;}
        .has-error{color: #ed5565;}
        .has-receive{color: #9E792E;}
        .remark {
            width: 100%; /*自动适应父布局宽度*/
            overflow: auto;
            word-break: break-all;
        }
    </style>
    <%}%>
</head>

<body>
<div class="wrapper wrapper-content">
    <div class="row">
        <div class="col-sm-12">
            <div class="ibox-content" style="padding:15px 20px 0px 20px;">
                <form id="search-form" role="form" class="form-horizontal" autocomplete="off">
                    <div class="form-group">

                        <label class="control-label col-sm-1">供应商:</label>
                        <div class="col-sm-2">
                            <input type="text" id="produceSupplierName" name="produceSupplierName" class="form-control">
                        </div>
                        <label class="control-label col-sm-1">PO号:</label>
                        <div class="col-sm-2">
                            <input type="text" id="produceNo" name="produceNo" class="form-control">
                        </div>
                        <label class="control-label col-sm-1">柜号:</label>
                        <div class="col-sm-2">
                            <input type="text" id="createPlanNo" name="createPlanNo" class="form-control">
                        </div>
                        <a class="search-btn btn btn-success" style="margin-bottom: 0px;">查询</a>
                    </div>
                </form>
            </div>
            <div id="toolbar" class="btn-group" style="margin-bottom:5px;">
                <button  class="layui-btn layui-btn-sm import-btn" style="margin-left: -2px"  id="import-btn"  name="import-btn">
                    <i class="layui-icon"></i>批量付款
                </button>

                <button  class="layui-btn  layui-btn-sm" >
                    <i class="fa fa-download"></i>
                    <a href="/excel/批量付款模板.xlsx" style="color: white" download>模板下载</a>
                </button>

                <button  class="export-btn layui-btn  layui-btn-sm" >
                    <i class="fa fa-download" aria-hidden="true"></i> 导出当前表头数据
                </button>
            </div>
    <!-- START 数据表格 -->
    <table id="portTable"
           data-toolbar="#toolbar"
           data-side-pagination="server"
           data-pagination="true"
           data-resizable="true"
           data-url="/sea/supply/produce/payment/findByCnd.do"
           data-page-size="15"
           class="table table-striped table-hover"
    >
    </table>
        </div>
    </div>
    <!-- END 数据表格 -->
</div>

<div id="layer_remark_window" style="display: none;overflow-x: hidden;overflow-y: hidden">
    <!-- START 内容部分 -->
    <div class="container" >
        <form class="form-horizontal" id="info-pay_check" method="post" autocomplete="off" style="margin: auto">
            <input type="hidden" id="id" name="id">

            <div class="form-group" style="margin-top: 20px">
                <label class="category control-label col-sm-2" style="max-width: 100px">备注</label>
                <div class="col-sm-6" style="max-width: 400px;">
                    <textarea id="producePaymentRemark" name="producePaymentRemark" class="remark form-control" rows="7"></textarea>
                </div>
            </div>
            <div class="form-group" style="margin-top: 20px">
                <label class="control-label col-sm-7" style="max-width: 100px"></label>
                <button type="button" class="save-remark-btn btn btn-primary">
                    <i class="fa fa-save"></i> 保存
                </button>
            </div>
        </form>
    </div>
    <!-- END 内容部分 -->
</div>

<%layout("/WEB-INF/view/common/inc/admin_js.html",{modules:'jquery,bootstrap,bootstrap-table,dtvalidate,dtvalidate-lang-zh_cn,bootstrap-table-zh-CN,laydate,layer,layui,upload,chosen,chosen-demo'}){}%>
<script>
    laydate.render({
        elem: '#produceOutTime' //指定元素
        ,trigger: 'click'
    });
    var $portTable = $('#portTable');
    $(function(){
        $(".check").hide();
        $("#produceIsCheck").change(function () {
            var produceIsCheck=$("#produceIsCheck").val();
            if(produceIsCheck=='否'){
                $(".check").hide();
            }else{
                $(".check").show();
            }
        });
        var cur_table='';
        //START 表格配置
        $portTable.bootstrapTable({
            detailView: true,
            columns: [

                {
                    title: '采购单号',
                    field: 'produceNo',
                    align: 'center',
                    valign: 'middle'
                },
                {
                    title: '供应商',
                    field: 'produceSupplierName',
                    align: 'center',
                    valign: 'middle'
                },

                {
                    title: '采购总量',
                    field: 'produceTotal',
                    align: 'center',
                    valign: 'middle'
                },{
                    title: '实收数量/出货金额',
                    field: '',
                    align: 'center',
                    valign: 'middle',
                    formatter:function (v,r) {
                        return r.produceTotal-r.produceSurplus+'/'+r.produceAlreadyPay;
                    }
                },
                {
                    title: '采购币种',
                    field: 'produceMoneyType',
                    align: 'center',
                    valign: 'middle'
                },
                {
                    title: '采购总价',
                    field: 'produceTotalMoney',
                    align: 'center',
                    valign: 'middle'
                },
                {
                    title: '预付款',
                    field: 'producePayment',
                    align: 'center',
                    valign: 'middle'
                },{
                    title: '待核销',
                    field: 'produceWaitPay',
                    align: 'center',
                    valign: 'middle'
                },
                {
                    title: '已申请金额',
                    field: 'producePay',
                    align: 'center',
                    valign: 'middle'
                },
                {
                    title: '未申请金额',
                    field: '',
                    align: 'center',
                    valign: 'middle',
                    formatter:function (v,r) {
                        var num=r.produceAlreadyPay-r.producePay;
                        return num.toFixed(2);
                    }
                },
                {
                    title: '已付款金额',
                    field: 'producePayMoney',
                    align: 'center',
                    valign: 'middle'
                },
                {
                    title: '备注',
                    field: 'producePaymentRemark',
                    align: 'center',
                    valign: 'middle'
                },
                {
                    title: '付款异常',
                    field: 'producePaymentStatus',
                    align: 'center',
                    valign: 'middle',
                    formatter:function (v,r) {
                        if(v==1){
                            return '无异常';
                        }else{
                            return '';
                        }
                    }
                },
                {
                    field: 'operate',
                    title: '操作',
                    align: 'center',
                    valign: 'middle',
                    formatter: optFormatter
                }
            ],
            onLoadSuccess : function(){
                //新增
                $('.pay-btn').on('click',function(){//新增
                    var id = $(this).attr('data-val');
                    layer.open({
                        title: false,
                        closeBtn: 0,
                        type: 2,
                        area: ['100%','100%'],
                        skin: 'layer-ext-espresso',
                        content: ['/sea/supply/produce/payment/addPage.do?produceId='+id, 'yes']
                    });
                });

                $('.remark-btn').on('click',function(){//新增
                    var id = $(this).attr('data-val');
                    var remark = $(this).attr('data-val-remark');
                    $("#id").val(id);
                    if(null == remark || remark=='null' || remark=='undefined'){
                        $("#producePaymentRemark").val('');
                    }else{
                        $("#producePaymentRemark").val(remark);
                    }

                    layer.open({
                        type:1,
                        title:'备注',
                        area:['600px','450px'],
                        skin:'layui-layer-lan',//layui-layer-molv ;layui-layer-lan;layui-layer-rim;layer-window
                        shadeClose:false,
                        resize: false,
                        scrollbar: false,
                        maxmin: false,//出现最大化按钮
                        content:$('#layer_remark_window')
                    })
                });

                //删除数据字典详情信息
                $('.receive-close-btn').on('click',function(){
                    var delid = $(this).attr('data-val'),delname = $(this).attr('data-val-no'),number=$(this).attr('data-val-number');
                    //删除提示
                    layer.confirm('确认当前采购单无异常?', {
                        btn: ['确定','取消'],title:'提示'
                    }, function(){
                        $.post('/sea/supply/produce/updatePaymentRemark.do',{id: delid,producePaymentStatus:1},function(data){
                            if( data.ok ){
                                layer.msg(data.msg,{icon: 1,time: 1500},function(){
                                    //刷新页面
                                    $portTable.bootstrapTable('refresh');
                                });
                            }else{
                                layer.msg(data.msg,{icon: 0,time: 1500});
                            }
                        });
                    });
                });
            },
            onExpandRow: function (index, row, $detail) {
                InitSubTable(index, row, $detail);
            }
        });


        var produceStatus;
        function InitSubTable (index, row, $detail) {
            var parentId = row.id;
            produceStatus = row.produceStatus;
            cur_table = $detail.html('<table id="child_table" class="table table-striped table-hover"></table>').find('table');
            $(cur_table).bootstrapTable({
                url: '/sea/supply/produce/payment/findByProduceId',
                method: 'get',
                clickToSelect: true,
                queryParams: {produceId: parentId},
                ajaxOptions: {produceId: parentId},
                //height: 500,
                uniqueId: "id",
                pageSize: 10,
                pageList: [10, 25],
                columns: [
                    {
                        field: 'produceIsCheck',
                        title: '核销',
                        align: 'center'
                    },
                    {
                        field: 'produceCheckMoney',
                        title: '核销金额',
                        align: 'center'
                    },
                    {
                        field: 'producePayment',
                        title: '出货金额',
                        align: 'center'
                    },

                    {

                        field: 'produceUnusualMoney',
                        title: '杂项费',
                        align: 'center'
                    },
                    {
                        field: '',
                        title: '实际付款金额',
                        align: 'center',
                        formatter: function (v, r) {
                            return (r.producePayment - r.produceCheckMoney - r.produceUnusualMoney).toFixed(2);
                        }
                    },
                    {
                        field: 'produceRemark',
                        title: '备注',
                        align: 'center'
                    },
                    {
                        title: '状态',
                        field: 'flag',
                        align: 'center',
                        valign: 'middle',
                        formatter: function (v, r) {
                            if (v == 0) {
                                return '未付款';
                            } else {
                                return '已付款';
                            }
                        }
                    },
                    {
                        field: 'produceOutTime',
                        title: '出货时间',
                        align: 'center'
                    },
                    {
                        field: 'createPlanNo',
                        title: '柜号',
                        align: 'center'
                    },
                    {
                        field: 'produceReason',
                        title: '备注',
                        align: 'center'
                    },
                    {
                        field: 'createTime',
                        title: '创建时间',
                        align: 'center'
                    }, {
                        field: 'operate',
                        title: '操作',
                        align: 'center',
                        valign: 'middle',
                        formatter: optFormatter2
                    }],
                onLoadSuccess: function () {
                    //修改
                    $('.edit-btn').on('click', function () {
                        var id = $(this).attr('data-val');
                        layer.open({
                            type: 2,
                            area: ['500px', '500px'],
                            content: ['/sea/supply/produce/payment/editPage.do?id=' + id, 'yes']
                        });
                    });

                    $('.del-btn').on('click',function(){
                        var id = $(this).attr('data-val');
                        layer.confirm('确定要删除当前？', {
                            btn: ['确定','取消'],title:'提示'
                        }, function(){
                            $.post('/sea/supply/produce/payment/delete.do',{id: id},function(data){
                                if( data.ok ){
                                    layer.msg(data.msg,{icon: 1,time: 1500},function(){
                                        //刷新页面
                                        cur_table.bootstrapTable('refresh');
                                    });
                                }else{
                                    layer.msg(data.msg,{icon: 0,time: 1500});
                                }
                            });
                        });
                    });



                }
            });
        }



        function optFormatter2(value,row){
            var content ='';
            content += '<button class="edit-btn btn btn-xs btn-info" data-val="' + row.id + '" data-produce-id="' + row.produceId + '" data-unit-price="' + row.produceUnitPrice + '" data-price="' + row.producePrice + '" data-total="' + row.produceTotal + '" data-sku="' + row.orderSku + '" data-val="' + row.id + '" title="修改"><i class="fa fa-edit"></i>  </button>';
            if(row.flag==0) {
                content += '<button class="del-btn btn btn-xs btn-danger" data-val="' + row.id + '" title="删除" style="margin-left: 5px;"><i class="fa fa-trash"></i>  </button>';
            }
            return content;
        }


        //操作菜单
        function optFormatter(value,row){
            var content = '';
            content += '<button class="remark-btn btn btn-xs btn-warning" data-val-remark="'+row.producePaymentRemark+'" data-val-no="' + row.produceNo + '" data-val="' + row.id + '" title="填写备注"><i class="fa fa-check-square-o" aria-hidden="true"></i> 填写备注 </button>';
            if(row.producePaymentStatus!='1') {
                content += '&nbsp;<button class="receive-close-btn btn btn-xs btn-danger" data-val-total="' + row.produceTotal + '"  data-val-no="' + row.produceNo + '" data-val="' + row.id + '" title="确认无异常"><i class="fa fa-check-square-o" aria-hidden="true"></i> 确认无异常 </button>';
            }
            content += '&nbsp;<button class="pay-btn btn btn-xs btn-success" data-val-no="' + row.produceNo + '" data-val="' + row.id + '" title="添加付款信息"><i class="fa fa-check-square-o" aria-hidden="true"></i> 添加付款信息 </button>';
            return content;
        }
        //END 表格配置
    });


    $('.search-btn').on('click',function(){
        $portTable.bootstrapTable('refresh', { url:'${ctxPath}/sea/supply/produce/payment/findByCnd.do?'+$('#search-form').serialize() });
    });

    $('.save-remark-btn').on('click',function() {
        layer.confirm('确定要修改当前数据？', {
            btn: ['确定', '取消'], title: '提示'
        }, function () {
            layer.closeAll();
            var index = layer.load();
            $.post('/sea/supply/produce/updatePaymentRemark', {
                id: $("#id").val(),
                producePaymentRemark: $("#producePaymentRemark").val()
            }, function (data) {
                if (data.ok) {
                    $portTable.bootstrapTable('refresh');
                    layer.msg(data.msg, {icon: 1, time: 1500, shade: [0.8, '#393D49']});
                    layer.close(index);
                } else {
                    layer.msg(data.msg, {icon: 0, time: 1500, shade: [0.8, '#393D49']});
                    layer.close(index);
                }
            });
        });
    });
    //提交表单
    $('.save-pay-btn').on('click',function(){
        var producePayment=$("#producePayment").val();
        if(producePayment=='' || producePayment==null){
            layer.msg("预付款不能为空！",{icon:1,time:1500,shade: [0.8, '#393D49']});
            return;
        }
        var produceIsCheck=$("#produceIsCheck").val();
        if(produceIsCheck=='是'){
            var produceCheckMoney=$("#produceCheckMoney").val();
            if(produceCheckMoney =='' || produceCheckMoney == null || produceCheckMoney==0){
                layer.msg("核销金额不能为空或者0！",{icon:1,time:1500,shade: [0.8, '#393D49']});
                return;
            }


        }
        layer.confirm('确定要预付款当前数据？', {
            btn: ['确定','取消'],title:'提示'
        }, function(){
            layer.closeAll();
            var index=layer.load();
            $.post('/sea/supply/produce/payment/save.do',{produceId:$("#pay_produce_id").val(),
                                                          producePayment:producePayment,
                                                          produceRemark:'付款',
                                                          produceIsCheck:$("#produceIsCheck").val(),
                                                          produceCheckMoney:$("#produceCheckMoney").val(),
                                                          produceIsBill:$("#produceIsBill").val(),
                                                          produceBillFile:$("#produceBillFile").val(),
                                                          produceOutTime:$("#produceOutTime").val()},function(data){
                if( data.ok ){
                    $portTable.bootstrapTable('refresh');
                    layer.msg(data.msg,{icon:1,time:1500,shade: [0.8, '#393D49']});
                    layer.close(index);
                }else{
                    layer.msg(data.msg,{icon: 0,time:1500,shade: [0.8, '#393D49']});
                    layer.close(index);
                }
            });
        });
    });

    $("#importSession").on("change",function(){
        var formFile = new FormData($('#info-pay_check')[0]);
        var index=layer.load(1, {
            shade: [0.1,'#fff'],
            zIndex : 1111//0.1透明度的白色背景
        });
        $.ajax({
            url: "/upload/uploadFile",
            data: formFile,
            type: "post",
            processData: false,//用于对data参数进行序列化处理 这里必须false
            contentType: false, //必须
            success: function (data) {
                console.log(data);
                if(data.ok){
                    parent.layer.msg("上传成功！",{icon: 0,time:1500,shade: [0.8, '#393D49']});
                    $("#produceBillFile").val(data.data);
                    layer.close(index);
                }else{
                    parent.layer.msg(data.msg,{icon: 0,time:1500,shade: [0.8, '#393D49']});
                    layer.close(index);
                }
            },
        });
    });


    layui.use('upload', function(){
        var upload = layui.upload;
        upload.render({
            elem: '#import-btn', //绑定元素,
            accept:'file',
            url: '/sea/supply/produce/payment/import/', //上传接口
            before:function () {
                layer.load(1, {
                    shade: [0.1,'#fff'] //0.1透明度的白色背景
                });
            }
            ,done: function(data){
                if( data.ok ){
                    layer.msg(data.msg,{icon: 1,time: 1500},function(){
                        //刷新页面
                        $portTable.bootstrapTable('refresh');
                        layer.closeAll();
                    });
                }else{
                    layer.msg(data.msg,{icon: 0,time: 5000},function () {
                        layer.closeAll();
                    });

                }
            }
            ,error: function(){
                //请求异常回调
                layer.closeAll();
            }
        });
    });

    $(".export-btn").click(function () {
        window.location.href='/sea/supply/produce/payment/export?'+$('#search-form').serialize();
    });
</script>
</body>
</html>
